﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Eflong
{
    public class EflongTryIco : Form , IForm
    {
        private Icon tryIco;

        private NotifyIcon notifyIco;

        private ContextMenu notifyIcoMnu;

        public string ApplicationName = "飞龙在线监控程序";

        public string ContextName = "显示内容";

        public string QuitName = "退出程序";

        public bool bRestart = false;

        public bool IsRestart { get { return bRestart; } }

        /// <summary>
        /// 初始化托盘
        /// </summary>
        /// <param name="IcoName">图标文件名</param>
        /// <param name="bMenu">是否要菜单</param>
        /// <param name="appName">应用程序名</param>
        /// <param name="content">显示内容</param>
        /// <param name="quit">退出显示</param>
        public void InitTryIco( string IcoName , bool bMenu , string appName , string content , string quit )
        {
            ApplicationName = appName;
            ContextName = content;
            QuitName = quit;
            InitTryIco( IcoName , bMenu );
        }

        /// <summary>
        /// 初始化托盘
        /// </summary>
        public void InitTryIco( string IcoName , bool bMenu )
        {
            notifyIco = new NotifyIcon( );
            notifyIco.Text = ApplicationName;
            try
            {
                tryIco = new Icon( IcoName );
                this.Icon = tryIco;
                notifyIco.Icon = tryIco;
            }
            catch ( Exception ex )
            {
                MessageBox.Show( "没有找到图标文件: " + ex.Message , "提示" );
            }
            notifyIco.Visible = true;
            notifyIco.BalloonTipText = "飞龙在线联网";
            if ( bMenu )
            {
                MenuItem[ ] mnuItems = new MenuItem[ 3 ];
                mnuItems[ 0 ] = new MenuItem( );
                mnuItems[ 0 ].Text = ContextName;
                mnuItems[ 0 ].Click += new EventHandler( onShowWindow_Click );
                mnuItems[ 1 ] = new MenuItem( );
                mnuItems[ 1 ].Text = "-";
                mnuItems[ 2 ] = new MenuItem( );
                mnuItems[ 2 ].Text = QuitName;
                mnuItems[ 2 ].Click += new EventHandler( onExit_Click );

                notifyIcoMnu = new ContextMenu( mnuItems );
                notifyIco.ContextMenu = notifyIcoMnu;
            }
        }

        private void onExit_Click( object sender , EventArgs e )
        {
            ExitSys( );
        }

        private void onShowWindow_Click( object sender , EventArgs e )
        {
            ShowApp( );
        }

        protected override void OnClosing( CancelEventArgs e )
        {
            //base.OnClosing(e);
            e.Cancel = true;
            HideApp( );
        }

        protected override void OnSizeChanged( EventArgs e )
        {
            base.OnSizeChanged( e );
            if ( this.WindowState == FormWindowState.Minimized )
            {
                Hide( );
                notifyIco.Visible = true;
            }
        }

        public void ShowApp( )
        {
            Show( );
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }

        public void HideApp( )
        {
            this.WindowState = FormWindowState.Minimized;
            Hide( );
            notifyIco.Visible = true;
            this.ShowInTaskbar = false;
        }

        /// <summary>
        /// 关闭应用前，进行清除应用准备
        /// </summary>
        protected virtual void CleanupApp( )
        {
            notifyIco.Visible = false;
            this.ShowInTaskbar = false;
        }

        protected virtual bool ExitSys( )
        {
            if ( IsQuit( ) )
            {
                CleanupApp( );
                Application.Exit( );
                return true;
            }
            return false;
        }

        /// <summary>
        /// 提示用户是否退出程序
        /// </summary>
        /// <returns></returns>
        protected virtual bool IsQuit( )
        {
            //if (MessageBox.Show(this, "您想退出系统吗?", "关闭提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            //{
            //    return true;
            //}
            //return false;

            LoginForm form = new LoginForm( );
            form.IsQuit = true;
            form.Text = this.Text;
            form.StartPosition = FormStartPosition.CenterScreen;
            DialogResult result = form.ShowDialog( );
            if ( result == DialogResult.Yes )
            {
                return true;
            }
            return false;
        }
    }
}